package rmi.globox;

import globox_db_server.TheaterManager;

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import utils.jn.Sit;

public class GloboxController {

	private Registry registry;
	private TheaterManager server;

	public GloboxController(String host, int port, String object) {

		// if (System.getSecurityManager() == null) {
		// System.setSecurityManager(new SecurityManager());
		// }

		try {
			registry = LocateRegistry.getRegistry(host, port);
			server = (TheaterManager) registry.lookup(object);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Iterable<String> GetTheaters() throws RemoteException,
			MalformedURLException, NotBoundException {

		try {

			Iterable<String> theaters = server.getTheaters();

			return theaters;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Iterable<Sit> makeReservation(String session, int transactionID)
			throws RemoteException, MalformedURLException, NotBoundException {

		try {

			Iterable<Sit> sits = server.makeReserve(session, transactionID);

			return sits;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Iterable<Sit> makeReservation(String session, int transactionID,
			Iterable<utils.jn.Sit> sits) throws RemoteException,
			MalformedURLException, NotBoundException {

		try {

			Iterable<Sit> result = server.makeReserve(session, transactionID,
					sits);

			return result;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean addPurchase(int transactionID) throws RemoteException,
			MalformedURLException, NotBoundException {

		try {

			boolean result = server.commit(transactionID);

			return result;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public Iterable<utils.jn.Sit> getSeatsByTheater(String theaterName) {
		// TODO Auto-generated method stub
		try {
			return server.getSeatsByTheater(theaterName);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}
